const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        height: 20,
        focus: false,
        sn: "",
        isClick: true,
        submitClick: true,
        isSubmit: 0
    },
    onLoad() {
        my.alert({
            title: '温馨提示',
            content: '如您反馈的是机柜问题，请扫描故障机柜二维码！',
            buttonText: '关闭',
            success: () => {
            }
        });
    },
    bindButtonTap() {
        this.onFocus();
    },
    onFocus() {
        this.setData({
            focus: true,
        });
    },
    onBlur() {
        this.setData({
            focus: false,
        });
    },

    bindTextAreaBlur(e) {
        console.log(e.detail.value);
    },
    bindFormSubmit(e) {
        let msg = e.detail.value.msg;
        if (msg == "") {
            my.alert({
                content: "请输入错误描述！"
            });
            return;
        }
        let that = this;
        let sn = that.data.sn;
        if (that.data.submitClick) {
            that.setData({
                submitClick: false
            })
            util.httpRequest('/feedback', {
                sn: sn,
                msg: msg
            }, function (res) {
                if (res.code == 1) {
                    that.setData({
                        submitClick: true
                    })
                    my.showToast({
                        content: `恭喜，您的问题已反馈成功，请耐心等待！`,
                        type: 'none'
                    });
                }
            })
        }
    },

    // ================== 扫码获取机柜sn ==========================================
    scanSn() {
        //调用二维码扫码功能
        let that = this;
        if (that.data.isClick) {
            that.setData({
                isClick: false
            })
            my.scan({
                type: 'qr',
                hideAlbum: true,
                success: function (res) {
                    if (res.qrCode) {
                        let url = res.qrCode;
                        let sn = url.split('/').pop();
                        console.log("扫码对应的SN=>" + sn);
                        that.setData({
                            isClick: false,
                            sn: sn
                        })
                    } else {
                        my.showToast({
                            content: '扫码失败',
                            type: 'none'
                        });
                    }
                },
                complete: function () {
                    that.setData({
                        isClick: true
                    })
                }
            });
        }
    }
});
